package cn.dnf.org.controller;

import cn.dnf.basic.annotation.PreAuthorize;
import cn.dnf.basic.util.JsonResult;
import cn.dnf.basic.util.PageList;
import cn.dnf.org.domain.Department;
import cn.dnf.org.query.DepartmentQuery;
import cn.dnf.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// @Controller
// @ResponseBody
@RestController // controller层，返回json格式数据
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 获取部门树
     * @return
     */
    @GetMapping("/deptTree")
    @PreAuthorize(name = "部门树",sn = "department:tree") //value后面用来做权限校验
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

    /**
     * http://localhost:8080/department  url
     * 动词： get
     * @return
     */
    // @RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping // 上面的结合
    @PreAuthorize(name = "所有部门",sn = "department:getAll")
    public List<Department> findAll(){

        return departmentService.loadAll();
    }

    /**
     * 查询一个  接收参数id
     *   url: http://localhost:8080/department/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询" )
    @PreAuthorize(name = "根据id部门",sn = "department:get")
    public Department findOne(@PathVariable("id") Long id){
        return departmentService.loadById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     *   JsonResult ：链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除" )
    @PreAuthorize(name = "删除部门",sn = "department:delete")
    public JsonResult deleteById(@PathVariable("id") Long id){

        try {
            departmentService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    /**
     * http://localhost:8080/department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    @PreAuthorize(name = "保存部门",sn = "department:save")
    public JsonResult updateOrAdd(@RequestBody Department department){

        try {
            if(department.getId()==null){
                // 添加操作
                departmentService.save(department);
            }else{
                // 修改操作
                departmentService.update(department);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     *  分页数据
     * @return
     */
    @ApiOperation(value = "分页查询接口" )
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        try {
            // 哈哈哈
            return departmentService.pageQuery(query);
            // ....
            // .....
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Department>();
        }
    }


    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){

        try {
            departmentService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }



}





















